
#include "Main.h"

bool Engine::CreateGLWindow(HINSTANCE inHinstance, App & inApp)
{
	GLuint pixelFormat;
	WNDCLASS wc;
	DWORD		dwExStyle;
	DWORD		dwStyle;
	memset(&wc,0,sizeof(WNDCLASS));
	wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
	wc.lpfnWndProc	= (WNDPROC) Engine::WndProc;
	wc.cbClsExtra	= 0;
	wc.cbWndExtra = 0;
	wc.hInstance = inHinstance;
	wc.hIcon	= LoadIcon(NULL, IDI_WINLOGO);
	wc.hCursor	= LoadCursor(NULL, IDC_ARROW);	
	wc.hbrBackground	= NULL;
	wc.lpszMenuName		= NULL;
	wc.lpszClassName	= inApp.mWindow->mTitle;

	RegisterClass(&wc);

	dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
	dwStyle=WS_OVERLAPPEDWINDOW|WS_CLIPSIBLINGS |WS_CLIPCHILDREN;

	inApp.mWindow->mHwnd = CreateWindowEx(dwExStyle,inApp.mWindow->mTitle,inApp.mWindow->mTitle,dwStyle,0,0,inApp.mWindow->mWindowRect->right - inApp.mWindow->mWindowRect->left,
		inApp.mWindow->mWindowRect->bottom - inApp.mWindow->mWindowRect->top,NULL,NULL,inApp.mHinstance,NULL);

	static	PIXELFORMATDESCRIPTOR pfd=				// pfd Tells Windows How We Want Things To Be
	{
		sizeof(PIXELFORMATDESCRIPTOR),				// Size Of This Pixel Format Descriptor
		1,											// Version Number
		PFD_DRAW_TO_WINDOW |						// Format Must Support Window
		PFD_SUPPORT_OPENGL |						// Format Must Support OpenGL
		PFD_DOUBLEBUFFER,							// Must Support Double Buffering
		PFD_TYPE_RGBA,								// Request An RGBA Format
		inApp.mWindow->mBits,										// Select Our Color Depth
		0, 0, 0, 0, 0, 0,							// Color Bits Ignored
		0,											// No Alpha Buffer
		0,											// Shift Bit Ignored
		0,											// No Accumulation Buffer
		0, 0, 0, 0,									// Accumulation Bits Ignored
		16,											// 16Bit Z-Buffer (Depth Buffer)  
		0,											// No Stencil Buffer
		0,											// No Auxiliary Buffer
		PFD_MAIN_PLANE,								// Main Drawing Layer
		0,											// Reserved
		0, 0, 0										// Layer Masks Ignored
	};

	inApp.mWindow->mHdc = GetDC(inApp.mWindow->mHwnd);
	pixelFormat = ChoosePixelFormat(inApp.mWindow->mHdc,&pfd);

	SetPixelFormat(inApp.mWindow->mHdc,pixelFormat,&pfd);

	inApp.mWindow->mHrc = wglCreateContext(inApp.mWindow->mHdc);

	wglMakeCurrent(inApp.mWindow->mHdc,inApp.mWindow->mHrc);

	ShowWindow(inApp.mWindow->mHwnd,SW_SHOW);						// Show The Window
	SetForegroundWindow(inApp.mWindow->mHwnd);						// Slightly Higher Priority
	SetFocus(inApp.mWindow->mHwnd);									// Sets Keyboard Focus To The Window

	return true;
}


GLvoid Engine::ReSizeGLScene(GLsizei inWidth, GLsizei inHeight)		// Resize And Initialize The GL Window
{
	if (inHeight==0)										// Prevent A Divide By Zero By
	{
		inHeight=1;										// Making Height Equal One
	}

	glViewport(0,0,inWidth,inHeight);						// Reset The Current Viewport

	glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
	glLoadIdentity();									// Reset The Projection Matrix

	// Calculate The Aspect Ratio Of The Window
	gluPerspective(45.0f,(GLfloat)inWidth/(GLfloat)inHeight,0.1f,1000.0f);

	glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix
	glLoadIdentity();									// Reset The Modelview Matrix
	
}


GLvoid Engine::KillGLWindow(App& inApp)								// Properly Kill The Window
{

	if (inApp.mWindow->mHrc)											// Do We Have A Rendering Context?
	{
		wglMakeCurrent(NULL,NULL);					// Are We Able To Release The DC And RC Contexts?
		
		wglDeleteContext(inApp.mWindow->mHrc);						// Are We Able To Delete The RC?
		
		inApp.mWindow->mHrc = NULL;										// Set RC To NULL
	}

	ReleaseDC(inApp.mWindow->mHwnd,inApp.mWindow->mHdc);					// Are We Able To Release The DC
		inApp.mWindow->mHdc = NULL;										// Set DC To NULL
	

	if(inApp.mWindow->mHwnd)
	{
		DestroyWindow(inApp.mWindow->mHwnd);
		inApp.mWindow->mHwnd = NULL;										// Set hWnd To NULL
	}

	UnregisterClass(inApp.mWindow->mTitle,inApp.mHinstance);			// Are We Able To Unregister Class
	inApp.mHinstance = NULL;
}